"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 统一数据库管理工具
Case Name   : 备节点宕机，开启最大可用模式&同步备
Create At   : 2022-07-07
Owner       : opentestcase007
Description : 
Expect      : 
History     : 
"""

import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger

Pri_U_SH = CommonSH('PrimaryDbUser')
Node_Num = Pri_U_SH.get_node_num()


@unittest.skipIf(3 > Node_Num, '不满足一主两备环境跳过')
class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.constant = Constant()
        self.com = Common()
        self.sh = CommonSH('PrimaryDbUser')
        self.dbuser_node = Node('dbuser')
        self.log.info(f"-----{os.path.basename(__file__)} start-----")

    def test_server_tools1(self):
        text = '----step0:查看enable_global_syscache初始配置值;----'
        self.log.info(text)
        self.init_para1 = self.com.show_param('most_available_sync')
        self.log.info(self.init_para1)
        text = '----step0:查看enable_thread_pool初始值;----'
        self.log.info(text)
        self.init_para2 = self.com.show_param('synchronous_commit')
        self.log.info(self.init_para2)

        text = '--step1:开启最大可用模式&同步备模式 expect:开启成功--'
        self.log.info(text)
        msg1 = self.sh.execute_gsguc('set',
                                     self.constant.GSGUC_SUCCESS_MSG,
                                     f"most_available_sync = on")
        self.assertTrue(msg1, '执行失败:' + text)
        msg2 = self.sh.execute_gsguc('set',
                                     self.constant.GSGUC_SUCCESS_MSG,
                                     f"synchronous_commit = on")
        self.assertTrue(msg2, '执行失败:' + text)

        text = '--step2:停止备1节点数据库 expect:设置成功--'
        self.log.info(text)
        cmctl_cmd = f'''source {macro.DB_ENV_PATH};
                    cm_ctl stop -n 2 -D {macro.DB_INSTANCE_PATH};'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        self.assertIn('cm_ctl: stop instance successfully', cmctl_cmd)

        text = '--step3:查看集群状态 expect:查看成功--'
        self.log.info(text)
        cmctl_cmd = f'''source {macro.DB_ENV_PATH};
                    cm_ctl query -v -C -i;'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        self.assertIn('6002     S Down    Manually stopped', cmctl_cmd)

        text = '--step4:主节点执行事务 expect:执行成功--'
        self.log.info(text)
        sql = f"create table t_cm_0010(id int,name varchar(100));" \
            f"insert into t_cm_0010 values(generate_series(1,1000)," \
            f"'a_'||generate_series(1,1000));" \
            f"update t_cm_0010 set name='cm' where id<200;" \
            f"delete from t_cm_0010;select count(*) from t_cm_0010;" \
            f"drop table t_cm_0010 cascade;"
        select_db_result = self.sh.execut_db_sql(sql)
        self.log.info(select_db_result)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, select_db_result)
        self.assertIn('INSERT 0 1000', select_db_result)
        self.assertIn('UPDATE 199', select_db_result)
        self.assertIn('DELETE 1000', select_db_result)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, select_db_result)

        text = '--step5:拉起备1节点数据库 expect:设置成功--'
        self.log.info(text)
        cmctl_cmd = f'''source {macro.DB_ENV_PATH};
                    cm_ctl start -n 2 -D {macro.DB_INSTANCE_PATH};'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        self.assertIn('cm_ctl: start instance successfully', cmctl_cmd)

        text = '--step6:恢复原集群状态 expect:恢复成功--'
        self.log.info(text)
        cmctl_cmd = f'''source {macro.DB_ENV_PATH};
                    cm_ctl switchover -a;'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        self.assertIn('cm_ctl: switchover successfully', cmctl_cmd)

    def tearDown(self):
        self.log.info('--无需清理环境--')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
